/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.shop.user.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.admin.api.vo.DeptExcelVo;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.excel.annotation.RequestExcel;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUserNickName;
import com.pig4cloud.pigx.shop.user.api.vo.ShopUserNickNameExcelVO;
import com.pig4cloud.pigx.shop.user.service.ShopUserNickNameService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 用户昵称库
 *
 * @author pigx code generator
 * @date 2023-12-13 19:30:48
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/nickname")
@Tag(description = "shop_user_nickname", name = "用户昵称库管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ShopUserNickNameController {

	private final ShopUserNickNameService shopUserNickNameService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param shopUserNickName 用户昵称库
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R getShopUserNickNamePage(Page page, ShopUserNickName shopUserNickName) {
		return R.ok(shopUserNickNameService.page(page, Wrappers.query(shopUserNickName)));
	}

	/**
	 * 通过id查询用户昵称库
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(shopUserNickNameService.getById(id));
	}

	/**
	 * 新增用户昵称库
	 * @param shopUserNickName 用户昵称库
	 * @return R
	 */
	@Operation(summary = "新增用户昵称库", description = "新增用户昵称库")
	@SysLog("新增用户昵称库")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('shop_user_nick_name_add')")
	public R save(@RequestBody ShopUserNickName shopUserNickName) {
		return R.ok(shopUserNickNameService.save(shopUserNickName));
	}

	/**
	 * 修改用户昵称库
	 * @param shopUserNickName 用户昵称库
	 * @return R
	 */
	@Operation(summary = "修改用户昵称库", description = "修改用户昵称库")
	@SysLog("修改用户昵称库")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('shop_user_nick_name_edit')")
	public R updateById(@RequestBody ShopUserNickName shopUserNickName) {
		return R.ok(shopUserNickNameService.updateById(shopUserNickName));
	}

	/**
	 * 通过id删除用户昵称库
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除用户昵称库", description = "通过id删除用户昵称库")
	@SysLog("通过id删除用户昵称库")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('shop_user_nick_name_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(shopUserNickNameService.removeById(id));
	}

	/**
	 * 导出excel 表格
	 * @param shopUserNickName 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('shop_user_nick_name_export')")
	public List<ShopUserNickName> export(ShopUserNickName shopUserNickName) {
		return shopUserNickNameService.list(Wrappers.query(shopUserNickName));
	}

	/**
	 * 导出空模板
	 * @return
	 */
	@ResponseExcel
	@GetMapping("/exportTemp")
	@PreAuthorize("@pms.hasPermission('shop_user_nick_name_export')")
	public List<ShopUserNickNameExcelVO> exportTemp() {
		List<ShopUserNickNameExcelVO> result = new ArrayList<>();
		ShopUserNickNameExcelVO vo = new ShopUserNickNameExcelVO();
		vo.setNickName("实例名称可删除");
		result.add(vo);
		return result;
	}

	/**
	 * 导入用户昵称
	 * @param excelVOList
	 * @param bindingResult
	 * @return
	 */
	@PostMapping("import")
	@PreAuthorize("@pms.hasPermission('shop_user_nick_name_export')")
	public R importUserNickName(@RequestExcel List<ShopUserNickNameExcelVO> excelVOList, BindingResult bindingResult) {
		return shopUserNickNameService.importUserNickName(excelVOList, bindingResult);
	}

}
